﻿Imports Actuariat
Imports Actuariat.comptes

Public Class PTIC_vueCompteDeResultat
    Inherits PTIC_vueCompte

    Protected _compteTechniqueVie As compteTechniqueVie_V2
    Protected _compteTechniqueNonVie As compteTechniqueNonVie_V2
    Protected _compteNonTechnique As compteNonTechnique_V2

#Region "Propriétés"
    Public Property compteTechniqueNonVie() As compteTechniqueNonVie_V2
        Get
            Return _compteTechniqueNonVie
        End Get
        Set(ByVal value As compteTechniqueNonVie_V2)
            If Not value Is Nothing Then
                _compteTechniqueNonVie = value
                refreshValeursBlocCompteTechniqueNonVie()
            End If
        End Set
    End Property
    Public Property compteTechniqueNonTechnique() As compteNonTechnique_V2
        Get
            Return _compteNonTechnique
        End Get
        Set(ByVal value As compteNonTechnique_V2)
            If Not value Is Nothing Then
                _compteNonTechnique = value
                refreshValeursBlocCompteNonTechnique()
            End If
        End Set
    End Property
    Public Property compteTechniqueVie() As compteTechniqueVie_V2
        Get
            Return _compteTechniqueVie
        End Get
        Set(ByVal value As compteTechniqueVie_V2)
            If Not value Is Nothing Then
                _compteTechniqueVie = value
                refreshValeursBlocCompteTechniqueVie()
            End If
        End Set
    End Property
#End Region

#Region "Initialisation"
    Protected Overrides Sub initNbLignes()
        For i As Integer = 1 To 46
            Me.Rows.Add()
            Me.Rows(i - 1).Height = 15
        Next
        'Cas particuliers
        Rows(3).Height = 6
        Rows(18).Height = 6
        Rows(34).Height = 6
    End Sub
    Protected Overrides Sub initTitresBlocs()
        initLignesBlocCompteTechVie()
        initLignesBlocCompteTechNonVie()
        initLignesBlocCompteNonTech()
    End Sub
    Protected Overrides Sub initLignesTitresGeneraux()
        Me.Rows(0).Cells(0).Value = "COMPTE DE RESULTAT"
    End Sub
    Private Sub initLignesBlocCompteTechVie()
        'Titre du compte technique vie
        Me.Rows(2).Cells(0).Value = "COMPTE TECHNIQUE VIE"
        Me.Rows(2).Cells(0).Style = styleCellulesTitre2()
        'Titre du compte de résultat
        Me.Rows(4).Cells(0).Value = "Produits"
        Me.Rows(5).Cells(0).Value = "   Primes DC"
        Me.Rows(6).Cells(0).Value = "   Provision DC ouverture"
        Me.Rows(7).Cells(0).Value = "   Produit des placements"
        Me.Rows(8).Cells(0).Value = "Charges"
        Me.Rows(9).Cells(0).Value = "   Charge des sinistres"
        Me.Rows(10).Cells(0).Value = "   Provision DC clôture"
        Me.Rows(11).Cells(0).Value = "   Frais de gestion"
        Me.Rows(12).Cells(0).Value = "   Charge des placements"
        Me.Rows(13).Cells(0).Value = "   Produit des placements transférés"
        Me.Rows(14).Cells(0).Value = "   Commission apporteur"
        Me.Rows(15).Cells(0).Value = "Résultat technique vie"
        Me.Rows(15).Cells(0).Style = styleCellulesAccentuees()

        Me.Rows(4).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(8).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub
    Private Sub initLignesBlocCompteTechNonVie()

        'Titre du compte de résultat
        Me.Rows(17).Cells(0).Value = "COMPTE TECHNIQUE NON VIE"
        Me.Rows(17).Cells(0).Style = styleCellulesTitre2()

        'Titre du compte de résultat
        Me.Rows(19).Cells(0).Value = "Produits"
        Me.Rows(20).Cells(0).Value = "   Primes IT TTC"
        Me.Rows(21).Cells(0).Value = "   Provision de rente ouverture"
        Me.Rows(22).Cells(0).Value = "   PPRC ouverture"
        Me.Rows(23).Cells(0).Value = "   Produit des placements alloués"
        Me.Rows(24).Cells(0).Value = "Charges"
        Me.Rows(25).Cells(0).Value = "   Taxe d'assurance"
        Me.Rows(26).Cells(0).Value = "   Sinistres réglés"
        Me.Rows(27).Cells(0).Value = "   Provision de rente clôture"
        Me.Rows(28).Cells(0).Value = "   PPRC clôture"
        Me.Rows(29).Cells(0).Value = "   Frais Natixis"
        Me.Rows(30).Cells(0).Value = "   Commission apporteur"
        Me.Rows(31).Cells(0).Value = "Résultat technique non vie"
        Me.Rows(31).Cells(0).Style = styleCellulesAccentuees()

        Me.Rows(19).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(24).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub
    Private Sub initLignesBlocCompteNonTech()
        'Titre du compte de résultat
        Me.Rows(33).Cells(0).Value = "COMPTE NON TECHNIQUE"
        Me.Rows(33).Cells(0).Style = styleCellulesTitre2()
        'Titre du compte de résultat
        Me.Rows(35).Cells(0).Value = "Produits"
        Me.Rows(36).Cells(0).Value = "   Résultat vie"
        Me.Rows(37).Cells(0).Value = "   Résultat non vie"
        Me.Rows(38).Cells(0).Value = "   Produit des placements alloués"
        Me.Rows(39).Cells(0).Value = "   Produit des placements (non vie)"
        Me.Rows(40).Cells(0).Value = "   Produit des placements transférés"

        Me.Rows(41).Cells(0).Value = "Charges"
        Me.Rows(42).Cells(0).Value = "   Charge des placements (non vie)"
        Me.Rows(43).Cells(0).Value = "   Impôts sur les bénéfices et taxes"
        Me.Rows(44).Cells(0).Value = "Résultat"
        Me.Rows(44).Cells(0).Style = styleCellulesAccentuees()

        Me.Rows(35).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
        Me.Rows(41).Cells(0).Style.Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Underline)
    End Sub
#End Region

#Region "Méthodes de dessin"

    Protected Overrides Sub paintBlocs(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        'CompteTechnique Vie
        paintBloc(e, 2, 15)
        'Compte technique non vie
        paintBloc(e, 17, 31)
        'Compte non technique
        paintBloc(e, 33, 44)
    End Sub

    Protected Overrides Function cellFormat(ByVal rowIndex As String, ByVal columnIndex As String) As String
        If rowIndex <> 0 And columnIndex <> 0 Then Return _formatDefaut Else Return ""
    End Function
    Protected Overrides Function cellBackColor(ByVal rowIndex As String, _
                                   ByVal columnIndex As String, _
                                   ByVal ligneHautBloc As Integer, _
                                   ByVal ligneBasBloc As Integer) As System.Drawing.Color
        If rowIndex = ligneHautBloc Then
            Return Color.FromArgb(231, 213, 231)
        ElseIf rowIndex = ligneBasBloc Then
            Return Color.FromArgb(255, 230, 30)
        Else
            Return Color.White
        End If
    End Function
    Protected Overrides Function cellAlignment(ByVal rowIndex As Integer, ByVal columnIndex As Integer) As StringAlignment
        If columnIndex = 0 Then Return StringAlignment.Near Else Return StringAlignment.Far
    End Function
    Protected Overrides Function cellFont(ByVal rowIndex As Integer, ByVal columnIndex As Integer, ByVal ligneHautBloc As Integer, ByVal ligneBasBloc As Integer) As Font
        If rowIndex = ligneHautBloc Or rowIndex = ligneBasBloc Then
            Return New System.Drawing.Font("Tahoma", 9, FontStyle.Bold)
        Else
            Return New System.Drawing.Font("Tahoma", 9, FontStyle.Regular)
        End If
    End Function

#End Region

#Region "Redirection des rafraichissements invoqués par les objets métiers"
    Public Overrides Function actualise(ByVal sender As Object, ByVal notification As Object) As PTIC_Util.resultatValidationContraintes
        'Actualisation des primes
        If TypeOf sender Is compteTechniqueVie_V2 Then
            actualiseFromCompteTechVie(sender)
        ElseIf TypeOf sender Is compteTechniqueNonVie_V2 Then
            actualiseFromCompteTechNonVie(sender)
        ElseIf TypeOf sender Is compteNonTechnique_V2 Then
            actualiseFromCompteNonTechnique(sender)
        ElseIf TypeOf sender Is ComptesMoteur2 Then
            'Actualiser depuis un changement de structure des comptes restitués
            actualiseFromComptesDeclines(sender, notification)
        End If
        Return Nothing
    End Function

    Public Sub actualiseFromCompteTechVie(ByVal compteTechVie As compteTechniqueVie_V2)
        refreshValeursBlocCompteTechniqueVie()
    End Sub
    Public Sub actualiseFromCompteTechNonVie(ByVal compteTechNonVie As compteTechniqueNonVie_V2)
        refreshValeursBlocCompteTechniqueNonVie()
        'MsgBox("Les cotisations et les frais de gestion sont MAJ dans le compte technique vie")
    End Sub
    Public Sub actualiseFromCompteNonTechnique(ByVal compteNonTechnique As compteNonTechnique_V2)
        refreshValeursBlocCompteNonTechnique()
    End Sub
#End Region

#Region "Rafraichissements élémentaires"

#Region "Rafraichissement de l'ossature"
    Private Sub refreshOssature()
        refreshNbColonnesProjection()
        refreshTitreGeneral()
    End Sub
    Private Sub refreshNbColonnesProjection()
        ColumnCount = _nbAnneesProjection + 1
        For Each c As DataGridViewColumn In Columns
            c.DefaultCellStyle.Format = "#,##0"
        Next
    End Sub
    Private Sub refreshTitreGeneral()
        'Années de projection
        For j As Integer = 1 To _nbAnneesProjection
            Me.Rows(0).Cells(j).Value = CStr(j) - 1 + _anneeDepart
        Next
    End Sub
#End Region

    Protected Overrides Sub refreshTitresGeneraux()
        'Années de projection
        For j As Integer = 1 To _nbAnneesProjection
            Me.Rows(0).Cells(j).Value = CStr(j - 1 + _anneeDepart)
        Next
    End Sub
    Protected Overrides Sub refreshValeursBlocs()
        refreshValeursBlocCompteTechniqueVie()
        refreshValeursBlocCompteTechniqueNonVie()
        refreshValeursBlocCompteNonTechnique()
    End Sub
    Private Sub refreshValeursBlocCompteTechniqueVie()
        If Not _compteTechniqueVie Is Nothing Then

            With _compteTechniqueVie
                If .etatCalcul <> PTIC_Util.valueEtatCalcul.NON_INITIALISE Then

                    Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None

                    For i As Integer = 1 To _nbAnneesProjection

                        With .resultats
                            'Les valeurs
                            If Not .primesDC Is Nothing Then Rows(5).Cells(i).Value = .primesDC(i - 1) / unite
                            If Not .PmDCouv Is Nothing Then Rows(6).Cells(i).Value = .PmDCouv(i - 1) / unite
                            If Not .produitPlacements Is Nothing Then Rows(7).Cells(i).Value = .produitPlacements(i - 1) / unite
                            If Not .sinRegles Is Nothing Then Rows(9).Cells(i).Value = -.sinRegles(i - 1) / unite
                            If Not .PmDCclot Is Nothing Then Rows(10).Cells(i).Value = -.PmDCclot(i - 1) / unite
                            If Not .FG Is Nothing Then Rows(11).Cells(i).Value = -.FG(i - 1) / unite
                            If Not .chargePlacements Is Nothing Then Rows(12).Cells(i).Value = -.chargePlacements(i - 1) / unite
                            If Not .produitPlacementsTransferes Is Nothing Then Rows(13).Cells(i).Value = -.produitPlacementsTransferes(i - 1) / unite
                            If Not .comApporteur Is Nothing Then Rows(14).Cells(i).Value = -.comApporteur(i - 1) / unite
                            If Not .resultatVie Is Nothing Then Rows(15).Cells(i).Value = .resultatVie(i - 1) / unite
                        End With
                    Next
                    Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                End If

            End With
        End If

    End Sub
    Private Sub refreshValeursBlocCompteTechniqueNonVie()
        If Not _compteTechniqueNonVie Is Nothing Then
            If _compteTechniqueNonVie.etatCalcul <> PTIC_Util.valueEtatCalcul.NON_INITIALISE Then
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                For i As Integer = 1 To _nbAnneesProjection
                    'Les valeurs
                    With _compteTechniqueNonVie.resultats
                        If Not .primesIT Is Nothing Then Me.Rows(20).Cells(i).Value = .primesIT(i - 1) / unite
                        If Not .pmRenteouv Is Nothing Then Me.Rows(21).Cells(i).Value = .pmRenteouv(i - 1) / unite
                        If Not .pmPPRCouv Is Nothing Then Me.Rows(22).Cells(i).Value = .pmPPRCouv(i - 1) / unite
                        If Not .produitPlacementsAlloues Is Nothing Then Me.Rows(23).Cells(i).Value = .produitPlacementsAlloues(i - 1) / unite
                        If Not .taxeAssu Is Nothing Then Me.Rows(25).Cells(i).Value = -.taxeAssu(i - 1) / unite
                        If Not .sinReglesIT Is Nothing Then Me.Rows(26).Cells(i).Value = -.sinReglesIT(i - 1) / unite
                        If Not .pmRenteClot Is Nothing Then Me.Rows(27).Cells(i).Value = -.pmRenteClot(i - 1) / unite
                        If Not .pmPPRCClot Is Nothing Then Me.Rows(28).Cells(i).Value = -.pmPPRCClot(i - 1) / unite
                        If Not .FG Is Nothing Then Me.Rows(29).Cells(i).Value = -.FG(i - 1) / unite
                        If Not .comApporteur Is Nothing Then Me.Rows(30).Cells(i).Value = -.comApporteur(i - 1) / unite
                        If Not .resultatsNonVie Is Nothing Then Me.Rows(31).Cells(i).Value = .resultatsNonVie(i - 1) / unite
                    End With
                Next
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
            End If
        End If
    End Sub
    Private Sub refreshValeursBlocCompteNonTechnique()
        If Not _compteNonTechnique Is Nothing Then
            If _compteNonTechnique.etatCalcul <> PTIC_Util.valueEtatCalcul.NON_INITIALISE Then
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                For i As Integer = 1 To _nbAnneesProjection
                    'Les valeurs
                    With _compteNonTechnique.resultats
                        If Not .resultatVie Is Nothing Then Me.Rows(36).Cells(i).Value = .resultatVie(i - 1) / unite
                        If Not .resultatNonVie Is Nothing Then Me.Rows(37).Cells(i).Value = .resultatNonVie(i - 1) / unite
                        If Not .produitPlacementsAlloues Is Nothing Then Me.Rows(38).Cells(i).Value = .produitPlacementsAlloues(i - 1) / unite
                        If Not .produitPlacements Is Nothing Then Me.Rows(39).Cells(i).Value = .produitPlacements(i - 1) / unite
                        If Not .produitPlacementsTransferes Is Nothing Then Me.Rows(40).Cells(i).Value = -.produitPlacementsTransferes(i - 1) / unite
                        If Not .chargePlacements Is Nothing Then Me.Rows(42).Cells(i).Value = -.chargePlacements(i - 1) / unite
                        If Not .impotsEtTaxes Is Nothing Then Me.Rows(43).Cells(i).Value = -.impotsEtTaxes(i - 1) / unite
                        If Not .resultat Is Nothing Then Me.Rows(44).Cells(i).Value = .resultat(i - 1) / unite
                    End With
                Next
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
            End If
        End If
    End Sub

#End Region

End Class